/**
 * 权限控制文件（后续添加预留接口）
 */
import router from './router'
import store from './store'
import NProgress from 'nprogress'
import useUser from './store/useUser'
import { getToken } from '@/utils/auth'
import getPageTitle from './utils/get-page-title'

NProgress.configure({ showSpinner: false })

const userStore = useUser(store)
const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  NProgress.start()
  // getLoginState().then(res => {
  //   if (res.data.hasOwnProperty('data')) {
  //     to.meta.title && (document.title = getPageTitle(to.meta.title))
  //     if (to.path === '/login') {
  //       next({ path: '/' })
  //     } else {
  //       next()
  //     }
  //   } else {
  //     whiteList.includes(to.path) ? next() : next(`/login?redirect=${to.path}`)
  //   }
  // })

  if (getToken()) {
    to.meta.title && (document.title = getPageTitle(to.meta.title))
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    whiteList.includes(to.path) ? next() : next(`/login?redirect=${to.path}`)
  }
})

router.afterEach(() => NProgress.done())
